{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Quickstart MedicalZoo.ipynb",
      "provenance": [],
      "mount_file_id": "1wJpLzfKOTHYXpQIOxVjB6d6U-VWnAqWA",
      "authorship_tag": "ABX9TyOTkj84KVvcdwCTCKq3zu8h",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/black0017/MedicalZooPytorch/blob/master/Quickstart_MedicalZoo.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "0EJ77EgqRPgf",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        ""
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "42NSNnzBRHeJ",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 1000
        },
        "outputId": "bb620582-20a4-4787-ac9d-8f341ef35050"
      },
      "source": [
        "!git clone https://github.com/black0017/MedicalZooPytorch\n",
        "!pip install -r ./MedicalZooPytorch/installation/requirements.txt"
      ],
      "execution_count": 1,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Cloning into 'MedicalZooPytorch'...\n",
            "remote: Enumerating objects: 530, done.\u001b[K\n",
            "remote: Counting objects: 100% (530/530), done.\u001b[K\n",
            "remote: Compressing objects: 100% (408/408), done.\u001b[K\n",
            "remote: Total 755 (delta 278), reused 317 (delta 110), pack-reused 225\u001b[K\n",
            "Receiving objects: 100% (755/755), 60.43 MiB | 11.64 MiB/s, done.\n",
            "Resolving deltas: 100% (359/359), done.\n",
            "Requirement already satisfied: scipy>=1.4.1 in /usr/local/lib/python3.6/dist-packages (from -r ./MedicalZooPytorch/installation/requirements.txt (line 1)) (1.4.1)\n",
            "Requirement already satisfied: numpy>=1.18.2 in /usr/local/lib/python3.6/dist-packages (from -r ./MedicalZooPytorch/installation/requirements.txt (line 2)) (1.18.2)\n",
            "Requirement already satisfied: torch==1.4.0 in /usr/local/lib/python3.6/dist-packages (from -r ./MedicalZooPytorch/installation/requirements.txt (line 3)) (1.4.0)\n",
            "Requirement already satisfied: nibabel>=3.0.2 in /usr/local/lib/python3.6/dist-packages (from -r ./MedicalZooPytorch/installation/requirements.txt (line 4)) (3.0.2)\n",
            "Requirement already satisfied: tensorboard>=2.2.0 in /usr/local/lib/python3.6/dist-packages (from -r ./MedicalZooPytorch/installation/requirements.txt (line 5)) (2.2.1)\n",
            "Requirement already satisfied: torchsummary>=1.5.1 in /usr/local/lib/python3.6/dist-packages (from -r ./MedicalZooPytorch/installation/requirements.txt (line 6)) (1.5.1)\n",
            "Collecting torchnet>=0.0.4\n",
            "  Downloading https://files.pythonhosted.org/packages/b7/b2/d7f70a85d3f6b0365517782632f150e3bbc2fb8e998cd69e27deba599aae/torchnet-0.0.4.tar.gz\n",
            "Requirement already satisfied: matplotlib>=3.2.1 in /usr/local/lib/python3.6/dist-packages (from -r ./MedicalZooPytorch/installation/requirements.txt (line 8)) (3.2.1)\n",
            "Requirement already satisfied: Pillow>=7.0.0 in /usr/local/lib/python3.6/dist-packages (from -r ./MedicalZooPytorch/installation/requirements.txt (line 9)) (7.0.0)\n",
            "Collecting torchsummaryX\n",
            "  Downloading https://files.pythonhosted.org/packages/36/23/87eeaaf70daa61aa21495ece0969c50c446b8fd42c4b8905af264b40fe7f/torchsummaryX-1.3.0-py3-none-any.whl\n",
            "Requirement already satisfied: wheel>=0.26; python_version >= \"3\" in /usr/local/lib/python3.6/dist-packages (from tensorboard>=2.2.0->-r ./MedicalZooPytorch/installation/requirements.txt (line 5)) (0.34.2)\n",
            "Requirement already satisfied: six>=1.10.0 in /usr/local/lib/python3.6/dist-packages (from tensorboard>=2.2.0->-r ./MedicalZooPytorch/installation/requirements.txt (line 5)) (1.12.0)\n",
            "Requirement already satisfied: grpcio>=1.24.3 in /usr/local/lib/python3.6/dist-packages (from tensorboard>=2.2.0->-r ./MedicalZooPytorch/installation/requirements.txt (line 5)) (1.28.1)\n",
            "Requirement already satisfied: absl-py>=0.4 in /usr/local/lib/python3.6/dist-packages (from tensorboard>=2.2.0->-r ./MedicalZooPytorch/installation/requirements.txt (line 5)) (0.9.0)\n",
            "Requirement already satisfied: google-auth<2,>=1.6.3 in /usr/local/lib/python3.6/dist-packages (from tensorboard>=2.2.0->-r ./MedicalZooPytorch/installation/requirements.txt (line 5)) (1.7.2)\n",
            "Requirement already satisfied: protobuf>=3.6.0 in /usr/local/lib/python3.6/dist-packages (from tensorboard>=2.2.0->-r ./MedicalZooPytorch/installation/requirements.txt (line 5)) (3.10.0)\n",
            "Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.6/dist-packages (from tensorboard>=2.2.0->-r ./MedicalZooPytorch/installation/requirements.txt (line 5)) (3.2.1)\n",
            "Requirement already satisfied: setuptools>=41.0.0 in /usr/local/lib/python3.6/dist-packages (from tensorboard>=2.2.0->-r ./MedicalZooPytorch/installation/requirements.txt (line 5)) (46.1.3)\n",
            "Requirement already satisfied: tensorboard-plugin-wit>=1.6.0 in /usr/local/lib/python3.6/dist-packages (from tensorboard>=2.2.0->-r ./MedicalZooPytorch/installation/requirements.txt (line 5)) (1.6.0.post3)\n",
            "Requirement already satisfied: google-auth-oauthlib<0.5,>=0.4.1 in /usr/local/lib/python3.6/dist-packages (from tensorboard>=2.2.0->-r ./MedicalZooPytorch/installation/requirements.txt (line 5)) (0.4.1)\n",
            "Requirement already satisfied: werkzeug>=0.11.15 in /usr/local/lib/python3.6/dist-packages (from tensorboard>=2.2.0->-r ./MedicalZooPytorch/installation/requirements.txt (line 5)) (1.0.1)\n",
            "Requirement already satisfied: requests<3,>=2.21.0 in /usr/local/lib/python3.6/dist-packages (from tensorboard>=2.2.0->-r ./MedicalZooPytorch/installation/requirements.txt (line 5)) (2.21.0)\n",
            "Collecting visdom\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/c9/75/e078f5a2e1df7e0d3044749089fc2823e62d029cc027ed8ae5d71fafcbdc/visdom-0.1.8.9.tar.gz (676kB)\n",
            "\u001b[K     |████████████████████████████████| 686kB 4.0MB/s \n",
            "\u001b[?25hRequirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.6/dist-packages (from matplotlib>=3.2.1->-r ./MedicalZooPytorch/installation/requirements.txt (line 8)) (0.10.0)\n",
            "Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib>=3.2.1->-r ./MedicalZooPytorch/installation/requirements.txt (line 8)) (1.2.0)\n",
            "Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib>=3.2.1->-r ./MedicalZooPytorch/installation/requirements.txt (line 8)) (2.4.7)\n",
            "Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib>=3.2.1->-r ./MedicalZooPytorch/installation/requirements.txt (line 8)) (2.8.1)\n",
            "Requirement already satisfied: pandas in /usr/local/lib/python3.6/dist-packages (from torchsummaryX->-r ./MedicalZooPytorch/installation/requirements.txt (line 10)) (1.0.3)\n",
            "Requirement already satisfied: rsa<4.1,>=3.1.4 in /usr/local/lib/python3.6/dist-packages (from google-auth<2,>=1.6.3->tensorboard>=2.2.0->-r ./MedicalZooPytorch/installation/requirements.txt (line 5)) (4.0)\n",
            "Requirement already satisfied: cachetools<3.2,>=2.0.0 in /usr/local/lib/python3.6/dist-packages (from google-auth<2,>=1.6.3->tensorboard>=2.2.0->-r ./MedicalZooPytorch/installation/requirements.txt (line 5)) (3.1.1)\n",
            "Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.6/dist-packages (from google-auth<2,>=1.6.3->tensorboard>=2.2.0->-r ./MedicalZooPytorch/installation/requirements.txt (line 5)) (0.2.8)\n",
            "Requirement already satisfied: requests-oauthlib>=0.7.0 in /usr/local/lib/python3.6/dist-packages (from google-auth-oauthlib<0.5,>=0.4.1->tensorboard>=2.2.0->-r ./MedicalZooPytorch/installation/requirements.txt (line 5)) (1.3.0)\n",
            "Requirement already satisfied: urllib3<1.25,>=1.21.1 in /usr/local/lib/python3.6/dist-packages (from requests<3,>=2.21.0->tensorboard>=2.2.0->-r ./MedicalZooPytorch/installation/requirements.txt (line 5)) (1.24.3)\n",
            "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.6/dist-packages (from requests<3,>=2.21.0->tensorboard>=2.2.0->-r ./MedicalZooPytorch/installation/requirements.txt (line 5)) (2020.4.5.1)\n",
            "Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /usr/local/lib/python3.6/dist-packages (from requests<3,>=2.21.0->tensorboard>=2.2.0->-r ./MedicalZooPytorch/installation/requirements.txt (line 5)) (3.0.4)\n",
            "Requirement already satisfied: idna<2.9,>=2.5 in /usr/local/lib/python3.6/dist-packages (from requests<3,>=2.21.0->tensorboard>=2.2.0->-r ./MedicalZooPytorch/installation/requirements.txt (line 5)) (2.8)\n",
            "Requirement already satisfied: tornado in /usr/local/lib/python3.6/dist-packages (from visdom->torchnet>=0.0.4->-r ./MedicalZooPytorch/installation/requirements.txt (line 7)) (4.5.3)\n",
            "Requirement already satisfied: pyzmq in /usr/local/lib/python3.6/dist-packages (from visdom->torchnet>=0.0.4->-r ./MedicalZooPytorch/installation/requirements.txt (line 7)) (19.0.0)\n",
            "Collecting jsonpatch\n",
            "  Downloading https://files.pythonhosted.org/packages/82/53/73ca86f2a680c705dcd1708be4887c559dfe9ed250486dd3ccd8821b8ccb/jsonpatch-1.25-py2.py3-none-any.whl\n",
            "Collecting torchfile\n",
            "  Downloading https://files.pythonhosted.org/packages/91/af/5b305f86f2d218091af657ddb53f984ecbd9518ca9fe8ef4103a007252c9/torchfile-0.1.0.tar.gz\n",
            "Collecting websocket-client\n",
            "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/4c/5f/f61b420143ed1c8dc69f9eaec5ff1ac36109d52c80de49d66e0c36c3dfdf/websocket_client-0.57.0-py2.py3-none-any.whl (200kB)\n",
            "\u001b[K     |████████████████████████████████| 204kB 14.4MB/s \n",
            "\u001b[?25hRequirement already satisfied: pytz>=2017.2 in /usr/local/lib/python3.6/dist-packages (from pandas->torchsummaryX->-r ./MedicalZooPytorch/installation/requirements.txt (line 10)) (2018.9)\n",
            "Requirement already satisfied: pyasn1>=0.1.3 in /usr/local/lib/python3.6/dist-packages (from rsa<4.1,>=3.1.4->google-auth<2,>=1.6.3->tensorboard>=2.2.0->-r ./MedicalZooPytorch/installation/requirements.txt (line 5)) (0.4.8)\n",
            "Requirement already satisfied: oauthlib>=3.0.0 in /usr/local/lib/python3.6/dist-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard>=2.2.0->-r ./MedicalZooPytorch/installation/requirements.txt (line 5)) (3.1.0)\n",
            "Collecting jsonpointer>=1.9\n",
            "  Downloading https://files.pythonhosted.org/packages/18/b0/a80d29577c08eea401659254dfaed87f1af45272899e1812d7e01b679bc5/jsonpointer-2.0-py2.py3-none-any.whl\n",
            "Building wheels for collected packages: torchnet, visdom, torchfile\n",
            "  Building wheel for torchnet (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "  Created wheel for torchnet: filename=torchnet-0.0.4-cp36-none-any.whl size=29744 sha256=230506de5d293edc718e91df2b92468f1ec18c52bb89bd684e5bfdcb8077b08e\n",
            "  Stored in directory: /root/.cache/pip/wheels/e1/03/fb/1c212c2f20905cdf97fe39022946cf16b8e66ed754a6663400\n",
            "  Building wheel for visdom (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "  Created wheel for visdom: filename=visdom-0.1.8.9-cp36-none-any.whl size=655250 sha256=54b2885d158380cdfef13368233b931ab4b59a31a7678fbec05e6d2dd68897d9\n",
            "  Stored in directory: /root/.cache/pip/wheels/70/19/a7/6d589ed967f4dfefd33bc166d081257bd4ed0cb618dccfd62a\n",
            "  Building wheel for torchfile (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "  Created wheel for torchfile: filename=torchfile-0.1.0-cp36-none-any.whl size=5712 sha256=7b4be361ef48f704a26d7704de910cfe59292b9bd1eddcad96370fd28ea4be5f\n",
            "  Stored in directory: /root/.cache/pip/wheels/b1/c3/d6/9a1cc8f3a99a0fc1124cae20153f36af59a6e683daca0a0814\n",
            "Successfully built torchnet visdom torchfile\n",
            "Installing collected packages: jsonpointer, jsonpatch, torchfile, websocket-client, visdom, torchnet, torchsummaryX\n",
            "Successfully installed jsonpatch-1.25 jsonpointer-2.0 torchfile-0.1.0 torchnet-0.0.4 torchsummaryX-1.3.0 visdom-0.1.8.9 websocket-client-0.57.0\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ZmsDH1FIXfcB",
        "colab_type": "text"
      },
      "source": [
        "## Extract datasets\n",
        "To train in any dataset extract the required datasets in `datasets`  folder, e.g., for iseg2017 extract your data here `MedicalZooPytorch/datasets/iseg_2017/iSeg-2017-Training/` \n",
        "\n",
        "in the following line I copy my data from google drive to datasets folder\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "0YmYHcJ4Yr-J",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "%cp -r '/content/drive/My Drive/MEDICAL/iseg_2017/iSeg-2017-Training/' '/content/MedicalZooPytorch/datasets/iseg_2017/'\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ir_bFiOgX_CI",
        "colab_type": "text"
      },
      "source": [
        "## Copy train file outside of test\n",
        "because of  relative imports of lib problem"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "MSZs1P07RVpn",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "5bc7a08f-4a4f-4c1d-d9a4-7b0b337d080b"
      },
      "source": [
        "%cd /content/MedicalZooPytorch\n",
        "%cp /content/MedicalZooPytorch/tests/train_with_trainer_class.py /content/MedicalZooPytorch/train_with_trainer_class.py \n"
      ],
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "/content/MedicalZooPytorch\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "GlgRjMq_iCxY",
        "colab_type": "text"
      },
      "source": [
        "# Start training"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "4HpkFJaDiDEN",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 318
        },
        "outputId": "105ab359-9932-43ad-8d13-1551bd63976d"
      },
      "source": [
        "! python train_with_trainer_class.py"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "SELECT subject with ID 1 for Validation\n",
            "Mode: train Subvolume samples to generate:  1000  Volumes:  9\n",
            "SELECT subject with ID 1 for Validation\n",
            "Mode: val Subvolume samples to generate:  100  Volumes:  1\n",
            "Full validation volume has been generated\n",
            "DATA SAMPLES HAVE BEEN GENERATED SUCCESSFULLY\n",
            "Building Model . . . . . . . .DENSENET2\n",
            "out: [1, 25, 25, 50, 50, 50, 75, 75, 75]\n",
            "in: [1, 26, 51, 101, 151, 201, 276, 351, 426]\n",
            "out: [1, 25, 25, 50, 50, 50, 75, 75, 75]\n",
            "in: [1, 26, 51, 101, 151, 201, 276, 351, 426]\n",
            "DENSENET2 Number of params: 4775782\n",
            "Model transferred in GPU.....\n",
            "2020-04-24 09:35:15.252311: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.1\n",
            "START TRAINING...\n"
          ],
          "name": "stdout"
        }
      ]
    }
  ]
}